home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 November / Macworld Nov ’95.toast / Developers / Selection ƒ 2.5 / obj_array+ < prev    next >
Encoding:
Text File  |  1994-10-30  |  2.1 KB  |  96 lines  |  [TEXT/MSET]

  1. $16 mybuffer
  2.  
  3. : $>selID    ( addr len -- n )
  4.     put: mybuffer
  5.     uc: mybuffer
  6.     get$: mybuffer
  7.     sel? not ?error 124
  8.     hash
  9.     0 -> dfrselID ;
  10.  
  11. :class  OBJ_ARRAY+  super{ OBJ_ARRAY } 32767 indexed
  12.  
  13. :m selfinit:  { \ dln slf -- }
  14.     idxBase 6 - w@  -> dln   self -> slf        \ Set up
  15.     limit  0
  16.     ?DO
  17.         slf  I ^elem dln cmove
  18.     LOOP ;m
  19.  
  20. \ applySel: needs the addr len of a selector
  21. \ e.g. " release:" applySel: tobject
  22. \ this would apply release: to each object in the array
  23.  
  24. :m applySel:  { addr len \ selID slf -- }    \ applies the given selector to each object
  25.     addr len $>selID -> selID
  26.     self -> slf
  27.     limit  0
  28.     ?DO
  29.         i select: slf
  30.         slf selid (bwith) EX-METHOD        \ we could make this faster
  31.     LOOP ;m
  32.  
  33. \ applycfa: needs the cfa of a word that sends a late bound message to an
  34. \ object that will be on the stack
  35. \ e.g.  : initRect  { obj -- }
  36. \            10 10 20 20 put: obj ;
  37. \        ' initRect applycfa: tobject
  38.     
  39. :m applycfa:  { cfa \ slf -- }    \ applies the given cfa with each object selected as current
  40.     self -> slf
  41.     limit  0
  42.     ?DO
  43.         i select: slf
  44.         slf cfa execute        \ we could make this faster
  45.     LOOP ;m
  46.  
  47. ;class
  48.  
  49. endload
  50.  
  51. *** EXAMPLE USE
  52.  
  53. :class var+ super{ var }
  54.  
  55. :m classinit:
  56.     4 put: super ;m
  57.    
  58. ;class
  59.  
  60. :class test super{ var+ OBJ_ARRAY+ }
  61.  
  62. :m classinit:
  63.     classinit: super
  64.     selfinit: super ;m
  65.    
  66. ;class
  67.  
  68. 3 test v
  69.  
  70. " get:" applySel: v     \ verify that selfinit: worked
  71.  
  72. : put5  { obj -- }
  73.     5 put: obj ;
  74.  
  75. ' put5 applycfa: v
  76.  
  77. " get:" applySel: v      \ verify that applycfa: worked
  78.  
  79.  
  80. *** DESCRIPTION
  81.  
  82. Class obj_array is a great concept and works very well in most circumstances.  
  83. The problem with obj_array is that it assumes all ivar data want to be 
  84. initialized to values of zero.  In otherwords, only the index 0 ivar will 
  85. receive the classinit: message.  All other indexed ivars are set to zero.  
  86. Obj_array+ has a method, selfinit:, that assures all ivar data is properly 
  87. initialized.  
  88.  
  89. Any subclass of obj_array+ should contain a classinit: method that calls classinit: super
  90. and selfinit: super as shown in the above example.
  91.  
  92. We also have methods for applying either the same selector or the same xt (cfa) to
  93. every object in the array.
  94.  
  95. *** REVISION HISTORY
  96.